import { supabase } from '../lib/supabaseClient'

// 获取所有食谱
export const getRecipes = async () => {
  const { data, error } = await supabase.from('recipe').select()
  if (error) {
    throw error
  }
  return data
}

// 根据类型获取食谱
export const getRecipesByType = async (typeId) => {
  const { data, error } = await supabase
    .from('recipe')
    .select()
    .eq('pid', typeId)
  
  if (error) {
    throw error
  }
  return data
}

// 创建新食谱
export const createRecipe = async (recipeData) => {
  const { data, error } = await supabase
    .from('recipe')
    .insert([recipeData])
    .select()
  
  if (error) {
    throw error
  }
  return data[0]
}

// 更新食谱
export const updateRecipe = async (id, recipeData) => {
  const { data, error } = await supabase
    .from('recipe')
    .update(recipeData)
    .eq('id', id)
    .select()
  
  if (error) {
    throw error
  }
  return data[0]
}

// 删除食谱
export const deleteRecipe = async (id) => {
  const { error } = await supabase
    .from('recipe')
    .delete()
    .eq('id', id)
  
  if (error) {
    throw error
  }
  return true
}
